package com.mango.amazon;
import com.amazon.spapi.financesV0.model.AdjustmentItem;
import com.amazon.spapi.listingsItems_2021_08_01.model.PatchOperation.OpEnum;

import com.amazon.AmazonSpiSite;
import com.amazon.spapi.listingsItems_2021_08_01.ApiException;
import com.amazon.spapi.listingsItems_2021_08_01.model.FulfillmentAvailability;
import com.amazon.spapi.listingsItems_2021_08_01.model.Item;
import com.amazon.spapi.listingsItems_2021_08_01.model.ListingsItemPatchRequest;
import com.amazon.spapi.listingsItems_2021_08_01.model.ListingsItemPutRequest;
import com.amazon.spapi.listingsItems_2021_08_01.model.ListingsItemSubmissionResponse;
import com.amazon.spapi.listingsItems_2021_08_01.model.PatchOperation;
import com.amazon.spapi.merchantFulfillmentV0.model.Length;
import com.amazon.spapi.merchantFulfillmentV0.model.UnitOfLength;
import com.google.common.collect.Lists;
import com.mango.amazon.enums.OperationType;
import com.mango.amazon.model.feed.Messages;
import com.sun.jmx.snmp.Timestamp;
import com.sun.media.jfxmedia.logging.Logger;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据获取API
 */
@Service
public class ListingsItemsService {

    public void getListingsItem() throws ApiException {
        ArrayList<String> marketplaceIds = new ArrayList<>();
        marketplaceIds.add(AmazonSpiSite.US.getMarketplaceId());
        ArrayList<String> arrayList = new ArrayList<>();
        arrayList.add("attributes");
       //  arrayList.add("summaries");
        arrayList.add("issues");
        arrayList.add("offers");
        arrayList.add("fulfillmentAvailability");
        arrayList.add("procurement");
        Item akia5VN36NQQVWDQKU5Z = APIfactory.listingsApi.getListingsItem("AMQQQUR21JKL3", "TEST-SKU-ZI-11", marketplaceIds, null, arrayList);
        System.out.println(akia5VN36NQQVWDQKU5Z);
    }


    public void addListingsItem(Map map) throws ApiException {
        List<String> marketplaceIds = new ArrayList<>();
        marketplaceIds.add("ATVPDKIKX0DER");

        ListingsItemPutRequest listingsItemPutRequest = new ListingsItemPutRequest();
        listingsItemPutRequest.setRequirements(ListingsItemPutRequest.RequirementsEnum.LISTING);
        listingsItemPutRequest.setProductType("BOOKEND");
        listingsItemPutRequest.setAttributes(map);

        long time = new Date().getTime();
        System.out.println(time);
        ListingsItemSubmissionResponse amqqqur21JKL3 = APIfactory.listingsApi.putListingsItem("AMQQQUR21JKL3", "SUK-TEST2-addItem", marketplaceIds, listingsItemPutRequest, null);

        System.out.println(amqqqur21JKL3);
    }

    /**
     * 补丁 库存信息
     */
    public void patchListingsItem() throws ApiException {
        ListingsItemPatchRequest listingsItemPatchRequest = new ListingsItemPatchRequest();
        listingsItemPatchRequest.setProductType("BOOKEND");


        List<PatchOperation> patchOperations = new ArrayList<>();
        PatchOperation patchOperation = new PatchOperation();
        patchOperation.setOp(OpEnum.ADD);
        patchOperation.setPath("/attributes/fulfillment_availability");

        // 库存
        Map fulfillmentAvailability = new HashMap<String, Object>();
        fulfillmentAvailability.put("quantity",new BigInteger("10"));
        fulfillmentAvailability.put("is_inventory_available", true);
        fulfillmentAvailability.put("fulfillment_channel_code", "DEFAULT");
        fulfillmentAvailability.put("lead_time_to_ship_max_days", 5);

        List<Object> objects = Lists.newArrayList();
        // adjustmentItem.setFulfillmentChannelCode("AMAZON_NA");

        objects.add(fulfillmentAvailability);
        patchOperation.setValue(objects);
        patchOperations.add(patchOperation);


        listingsItemPatchRequest.setPatches(patchOperations);

        List<String> marketplaceIds = new ArrayList<>();
        marketplaceIds.add("ATVPDKIKX0DER");
        System.out.println();
        ListingsItemSubmissionResponse amqqqur21JKL3 = APIfactory.listingsApi.patchListingsItem("AMQQQUR21JKL3", "TEST-SKU-ZI-11", marketplaceIds, listingsItemPatchRequest, null);
        System.out.println(amqqqur21JKL3);
    }
}
